package com.crm.controller;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.crm.entity.Askers;
import com.crm.entity.Netaskers;
import com.crm.entity.Student;
import com.crm.repository.NetaskersRepository;
import com.crm.service.AskersService;
import com.crm.service.NetaskersService;
import com.crm.service.StudentService;
import com.crm.service.impl.NetaskersServiceImpl;
import com.crm.util.SuiJiDaLuanList;

@RestController
@RequestMapping("/netAsker")
public class NetaskersController {

	@Autowired
	private NetaskersService  service;
	@Autowired
	private NetaskersRepository repository;
	@Autowired
	private AskersService askersService;
	@Autowired
	private StudentService studentService;
	
	@GetMapping
	public Object getNetAskers(Netaskers n,Integer page,Integer limit) {
		return service.getNetAskerByFenYe(n, page, limit);
	}
	
	@PostMapping
	public Object addNetAskers(Netaskers n) {
		return service.addNetaskers(n);
	}
	@PutMapping
	public Object updateNetAskers(Netaskers n) {
		//获取原先的值
		Optional<Netaskers> netAsker=repository.findById(n.getNetAskerId());
		Netaskers n1= netAsker.get();
		n1.setTaskNum(n.getTaskNum());
		n1.setWeight(n.getWeight());
		
		return service.updateNetaskers(n1);
	}
	@DeleteMapping
	public Object delNetAskers(Integer id) {
		return service.deleteNetaskers(id);
	}
	@GetMapping("/test")
	public Object  AutoFenPei(String userId,String status) {
		System.out.println("userId"+userId);
		//通过用户id查询网络咨询师id
		Integer netAskerId=service.netId(userId);
		//判断状态 是:代表打开了自动分配开关 否:代表关闭了开关
		if(status.equals("是")) {
			//修改网络咨询师的状态
			service.status(status,netAskerId);
			//这样可以取一个整数出来DecimalFormat   df=new DecimalFormat("#");
			//这样是取小数点后一位
			DecimalFormat   df=new DecimalFormat("#.#");  
			//取double的整数部分
			DecimalFormat   sdf=new DecimalFormat("#"); 
			//学生总数
			int count=service.StuCount(netAskerId);
			//学生数据
			List<Student> slist = studentService.Cha(netAskerId);
			//有多少咨询师 and 咨询师的数据(已签到的咨询师)
			List<Askers> alist = askersService.GetQianDaoAll();
			//用来存放咨询师的权重

            SuiJiDaLuanList suiji = new SuiJiDaLuanList();
			alist = suiji.shuffle(alist);
			Double[] xuan = new Double[alist.size()];
			for(int i =0; i<alist.size();i++) {
				xuan[i]=(double)alist.get(i).getWeight();
			}
			//接收权重总比例(份数)
			double x=0.0;
			for (int i = 0; i < xuan.length; i++) {
				x+=xuan[i];
			}
			//用来存放根据权重计算出来的比例
			Double[] ssr = new Double[xuan.length];
			for(int i=0;i<xuan.length;i++) {
				String g=String.valueOf(df.format((xuan[i]/x)*count));
				String [] ds=String.valueOf(g).split("\\.");
				Integer s=0;
				//判断比例和总数相乘是否正好是一个整数(如果是整数下面的判断将不会执行,会执行下一个for循环)
				if(ds.length!=1) {
					s=Integer.parseInt(ds[1]);
				}
				//判断小数是否进行四舍五入
				if(s>5) {
						ssr[i]=(xuan[i]/x)*count+1;				
					}else {
						ssr[i]=(xuan[i]/x)*count;			
					}	
			}
			List<Integer> st= new ArrayList<Integer>();
			//上面得到的数据都是double类型的，这里把它转化为整数类型 就是分配的学生数量
			Integer jie=0;
			for(int i=0;i<ssr.length;i++) {
				String g=String.valueOf(df.format(ssr[i]));
				String [] ds=String.valueOf(g).split("\\.");
				//Integer s=Integer.parseInt(ds[1]);
				jie=Integer.parseInt(ds[0]);
				st.add(jie);
				System.out.println(jie);
			}
			List<String> sidlist = new ArrayList<String>();
			int t=0;
		
				sidlist.addAll(service.StuId(netAskerId));
			
					System.out.println(sidlist);
		

			//实现自动分配
					int c=0;
			for(int i =0;i<st.size();i++) {
			
				
				for(int j=0;j<st.get(i);j++) {
					c++;
				    System.out.println("我是I:"+i);
					System.out.println(c);
					if(i==0) {
						
						studentService.FenPeiStu(sidlist.get(j), alist.get(i).getAskerId());
						 
					} 
						if(i!=0) {
							
							studentService.FenPeiStu(sidlist.get(c-1), alist.get(i).getAskerId());	
						}
	
					}
			
			}
			return sidlist;
		}else {
			//修改网络咨询师的状态
			service.status(status,netAskerId);
			
			//这个1是为了前台点击开关时候方便判断提示的(返回值不为1代表自动分配成功,为1代表自动分配关闭)
			return "1";
		}
		
	}
}
